<?php

/*
 * Copyright (C) 2014 University of Pittsburgh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('util/Constants.inc');
require_once('AbstractDAO.inc');
require_once('Document.inc');

class DocumentDAO extends AbstractDAO {
  
  public function __construct() {
    parent::__construct();
  }
  
  public function select_doc_sec_ids($select_stmt = '') {
    $dbh = new PDO(Constants::DB_NAME, Constants::DB_USERNAME, Constants::DB_PASSWORD);
    $dbh->quote($select_stmt);
    $stmt = $dbh->prepare($select_stmt);
    
    if($stmt) {
    
      if($stmt->execute()) {
    
        $ret_val = array();
    
        foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $assoc_arr) {
          $doc_sec_id = $assoc_arr['doc_sec_id'];
          $ret_val[] = $doc_sec_id;
        }
    
        return $ret_val;
    
      } else {
        $this->error_code = $stmt->errorCode();
        $this->error_info = $stmt->errorInfo();
        unset($dbh);
        return NULL;
      }
    } else {
      $this->error_code = $dbh->errorCode();
      $this->error_info = $dbh->errorInfo();
      unset($dbh);
      return NULL;
    }
  }
  
  public function select_document_ids($select_stmt = '') {
    $dbh = new PDO(Constants::DB_NAME, Constants::DB_USERNAME, Constants::DB_PASSWORD);
    $dbh->quote($select_stmt);
    $stmt = $dbh->prepare($select_stmt);
  
    if($stmt) {
  
      if($stmt->execute()) {
  
        $ret_val = array();
  
        foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $assoc_arr) {
          $document_id = $assoc_arr['document_id'];
          $ret_val[] = $document_id;
        }
  
        return $ret_val;
  
      } else {
        $this->error_code = $stmt->errorCode();
        $this->error_info = $stmt->errorInfo();
        unset($dbh);
        return NULL;
      }
    } else {
      $this->error_code = $dbh->errorCode();
      $this->error_info = $dbh->errorInfo();
      unset($dbh);
      return NULL;
    }
  }

  public function find_document($document_id = '') {
    
    /*
     * document_id uuid NOT NULL,
     * document_citation character varying(60) NOT NULL,
     * document_date date,
     * document_header character varying(255),
     * document_body text,
     * document_submit_date timestamp without time zone,
     * document_name character varying(64),
     * document_type character varying(64),
     */
    $dbh = new PDO(Constants::DB_NAME, Constants::DB_USERNAME, Constants::DB_PASSWORD);
    $stmt = $dbh->prepare('SELECT T1.document_id, T1.document_citation, T1.document_date, ' .
                          'T1.document_header, T1.document_body, T1.document_submit_date ' .
                          'FROM lena.document_inv T1 WHERE T1.document_id = ?');
    
    $stmt->bindParam(1, $document_id, PDO::PARAM_STR);
  
    if($stmt) {
  
      if($stmt->execute()) {
  
        $ret_val = NULL;
  
        $assoc_arr = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if($assoc_arr) {   
          try {
            $ret_val = new Document();
            $ret_val->document_id = $assoc_arr['document_id'];
            $ret_val->document_citation = $assoc_arr['document_citation'];
            $ret_val->document_date = $assoc_arr['document_date'];
            //New lines converted to <br />
            $ret_val->document_header = nl2br($assoc_arr['document_header']); 
            // Multiple spaces converted to &nbsp; 
            // New lines converted to <br />  (?<=\x20)\x20|\x20(?=\x20)|^\x20
            $ret_val->document_body = preg_replace('/\s\s+?/', '&#160;', nl2br($assoc_arr['document_body']));           
            $ret_val->document_submit_date = $assoc_arr['document_submit_date'];
          } catch (Exception $e) {
            $this->error_code = 101;
            $this->error_info = $e->getMessage();
            return NULL;
          }

        } else {
          $this->error_code = Constants::ERR_CODE_DOCUMENT_NOT_FOUND;
          $this->error_info = Constants::ERR_MSG_DOCUMENT_NOT_FOUND;
        }
  
        return $ret_val;
  
      } else {
        $this->error_code = $stmt->errorCode();
        $this->error_info = $stmt->errorInfo();
        unset($dbh);
        return NULL;
      }
    } else {
      $this->error_code = $dbh->errorCode();
      $this->error_info = $dbh->errorInfo();
      unset($dbh);
      return NULL;
    }
  }  
}

?>